package com.approval.approvalapp.center.acitivty;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.approval.approvalapp.R;
import com.approval.approvalapp.bean.MoniterBean;
import com.approval.approvalapp.bean.ZdrWlISTbean;
import com.approval.approvalapp.center.adapter.ZdrwListAdapter;
import com.approval.approvalapp.util.SpUtil;
import com.approval.approvalapp.util.base.BaseActivity;
import com.approval.approvalapp.util.net.NetRequsetUtil;
import com.approval.approvalapp.util.net.interfacepac.NetDownResponse;
import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ZdrwActivity extends BaseActivity implements ZdrwListAdapter.ZdrwClickListener {

    @Bind(R.id.back_iv)
    ImageView backIv;
    @Bind(R.id.titleTv1)
    TextView titleTv1;
    @Bind(R.id.rv)
    RecyclerView rv;
    @Bind(R.id.empty_ll)
    LinearLayout em;

    List<MoniterBean> moniterBeanList = new ArrayList<>();
    private List<ZdrWlISTbean> zdrWlISTbeans;


    @Override
    public int getLayoutId() {
        return R.layout.activity_zdrw;
    }

    @Override
    public void initView() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("org_code", SpUtil.getString(this,
                SpUtil.ORG_CODE, ""));
        NetRequsetUtil.getInstance().netRequestPostJson("v1/center/task_list",
                jsonObject.toString(), new NetDownResponse() {
                    @Override
                    public void success(String str) {
                        zdrWlISTbeans = JSON.parseArray(str, ZdrWlISTbean.class);
                        rv.setLayoutManager(new LinearLayoutManager(ZdrwActivity.this));
                        ZdrwListAdapter zdrwListAdapter = new ZdrwListAdapter();
                        if (zdrWlISTbeans == null || zdrWlISTbeans.size() == 0){
                            em.setVisibility(View.VISIBLE);
                        }else {
                            em.setVisibility(View.GONE);
                        }
                        zdrwListAdapter.setZdrWlISTbeans(zdrWlISTbeans, ZdrwActivity.this);
                        rv.setAdapter(zdrwListAdapter);
                    }

                    @Override
                    public void errowithresponse(String str) {

                    }

                    @Override
                    public void erro() {

                    }

                    @Override
                    public void finish() {

                    }
                });

    }

    @Override
    public void initData() {

    }

    @OnClick(R.id.back_iv)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void click(final ZdrWlISTbean zdrWlISTbean) {
        List<String> list = zdrWlISTbean.getList();
        moniterBeanList.clear();
        OptionsPickerView pvOptions1 = new
                OptionsPickerBuilder(this, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3, View v) {
                //返回的分别是三个级别的选中位置
                Intent intent = new Intent(ZdrwActivity.this,ZdrwXQActivity.class);
                intent.putExtra("id",zdrWlISTbean.getID() + "");
                intent.putExtra("m",moniterBeanList.get(options1).getId());
                ZdrwActivity.this.startActivity(intent);
            }
        }).build();

        int t_type = zdrWlISTbean.getT_TYPE();
        if (t_type == 1) {
            //年
            for (int i = 0; i < list.size(); i++) {
                moniterBeanList.add(new MoniterBean(list.get(i) + "",
                        list.get(i) + "年"));
            }

        } else if (t_type == 2) {
            //季度
            for (int i = 0; i < list.size(); i++) {
                moniterBeanList.add(new MoniterBean(list.get(i) + "",
                        list.get(i) + "季度"));
            }
        } else if (t_type == 3) {
            //月
            for (int i = 0; i < list.size(); i++) {
                moniterBeanList.add(new MoniterBean(list.get(i) + "",
                        list.get(i) + "月"));
            }
        } else {
            //周
            for (int i = 0; i < list.size(); i++) {
                moniterBeanList.add(new MoniterBean(list.get(i) + "",
                        list.get(i) + "周"));
            }
        }

        pvOptions1.setPicker(moniterBeanList, null, null);
        pvOptions1.show();
    }
}
